package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.entity.MqIccidAndSimCard;
import com.wtwd.campus.entity.MqMessage;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.utils.ReceiverUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@MsgType(value = Constants.ICCID_AND_SIM)
public class SimCardAndIccidReceiverHandler implements IReceiverHandler {
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private DeviceLogService deviceLogService;

    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqIccidAndSimCard mqIccidAndSimCard = (MqIccidAndSimCard) mqMessage.getObject();
        String imei = mqIccidAndSimCard.getImei();
        String commandNumber = mqIccidAndSimCard.getCommandNumber();
        //处理指令回复的时间
        String deviceDate = mqIccidAndSimCard.getDeviceDate();
        String deviceTime = mqIccidAndSimCard.getDeviceTime();
        Long time = getTime(deviceDate, deviceTime);
        DeviceInfo deviceInfo = deviceInfoService.getDeviceByImei(imei);
        if (deviceInfo != null) {
            if (mqIccidAndSimCard.getMqLocationData() != null) {
                if (mqIccidAndSimCard.getMqLocationData().getLatitude() != 0 && mqIccidAndSimCard.getMqLocationData().getLongitude() != 0) {
                    //进行定位数据存储以及围栏告警记录的存储
                    deviceInfo = receiverUtils.dealLocation(mqIccidAndSimCard.getMqLocationData(), time, deviceInfo, 1);
                }
            }
            //判断是否获取成功
            String number = mqIccidAndSimCard.getPhone();
            if (!"13912345678".equals(number)) {
                deviceInfo.setM1Card(mqIccidAndSimCard.getIccid());
                deviceInfo.setSimCard(number);
            }
            //修改设备信息
            deviceInfo.setLastUpTime(System.currentTimeMillis());
            deviceInfoService.modifyDeviceInfo(deviceInfo);
            //删除缓存中的指令
            String delKey = Constants.ICCID_AND_SIM.concat("-").concat(commandNumber);
            receiverUtils.deleteCommandInRedis(imei, delKey);
            //完善指令下发记录
            receiverUtils.completeCommandRecord(Constants.ICCID_AND_SIM, imei, commandNumber, null, Constants.COMMAND_COMPLETE, time);
            //存储设备接收日志
            DeviceLog deviceLog = new DeviceLog();
            deviceLog.setCommandNumber(commandNumber);
            deviceLog.setSendUser(Constants.DEVICE_USER);
            deviceLog.setImei(imei);
            deviceLog.setBigType(Constants.MESS_TYPE);
            deviceLog.setType(Constants.ICCID_AND_SIM);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("设备号", imei);
            jsonObject.put("Iccid", mqIccidAndSimCard.getIccid());
            jsonObject.put("手机号", mqIccidAndSimCard.getPhone());
            jsonObject.put("经度", mqIccidAndSimCard.getMqLocationData().getLongitude());
            jsonObject.put("纬度", mqIccidAndSimCard.getMqLocationData().getLatitude());
            jsonObject.put("速度", mqIccidAndSimCard.getMqLocationData().getSpeed());
            jsonObject.put("方位", mqIccidAndSimCard.getMqLocationData().getOrientation());
            jsonObject.put("日期", deviceDate);
            jsonObject.put("设备状态", mqIccidAndSimCard.getDeviceStatus());
            deviceLog.setCmd(jsonObject.toJSONString());
            deviceLog.setSendTime(System.currentTimeMillis());
            deviceLog.setState(1);
            deviceLogService.addDeviceLog(deviceLog);
        } else {
            saveUnInStorage(imei);
            receiverUtils.cloaseChannel(imei);
        }
    }
}
